//
//  XBEmoticonAttachment.swift
//  EmotionKeyboardDemo
//
//  Created by mac on 15/12/27.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//

import UIKit

class XBEmoticonAttachment: NSTextAttachment {
    
    // 表情模型
    var emoticon: XBEmoticon
    
    func imageText(font: UIFont) ->NSAttributedString {
        
        image = UIImage(contentsOfFile: emoticon.pngPath!)
        // 附件的高度
        let lineHeight = font.lineHeight ?? 0
        bounds = CGRectMake(0, -4, lineHeight, lineHeight)
        // 图片文本
        let imageAttri = NSMutableAttributedString(attributedString: NSAttributedString(attachment: self))
        // 设置图片文本的字体属性
        imageAttri.addAttribute(NSFontAttributeName, value: font, range: NSMakeRange(0, 1))
        return imageAttri
    }

    // 构造方法
    init(emoticon: XBEmoticon) {
        self.emoticon = emoticon
        super.init(data: nil, ofType: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
